/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manager;

/**
 *
 * @author Khoa
 */
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.LayerManager;
import EntityGameObject.*;
import MainPack.MainMIDlet;

public class GameLayerManager extends LayerManager {

    private Insect[] leftInsects;
    private Insect[] rightInsects;
    private PlayPet pet;
    private Tongue[] tongues;
    private int cornerX;
    private int cornerY;
    private int displayHeight;
    private int displayWidth;
    private int currentLeftX;
    private Image MosImage;
    private Image BeeImage;
    private Image petImage;
    private Image tongueImageBody;
    private Image petNormal;
    private boolean isTongueActive;
    private boolean TongueReturn;
    private boolean Collide;
    private int index;
    private int NoOfInsect;
    private int Score;
    private int gameTime;

    public GameLayerManager(int cornerX, int cornerY, int width, int height,
            Image mosImage,Image beeImage, Image petImage, Image tongueImageBody, Image petNormal, int speedpet, int staminapet, int tonguelength, int levelPet) {
        this.tongueImageBody = tongueImageBody;
        this.petNormal = petNormal;
        this.MosImage = mosImage;
        this.BeeImage = beeImage;
        this.petImage = petImage;
        this.cornerX = cornerX;
        this.cornerY = cornerY;
        this.displayHeight = height;
        this.displayWidth = width;
        setViewWindow(0, 0, width, height);
        setPet(new PlayPet(petImage, displayWidth, displayHeight,speedpet,staminapet,levelPet));
        append(getPet());

        LoadGameContent(4,tonguelength);
        isTongueActive = false;
        TongueReturn = false;
        Collide = false;
        index = 0;

        Score = -1;
        NoOfInsect = 0;
        gameTime = 0;
        
    }

    public void LoadGameContent(int level, int tonguelength) {
        if (leftInsects == null) {
            leftInsects = new Insect[level];
            for (int i = 0; i < leftInsects.length; i++) {
                leftInsects[i] = new Insect(MosImage, BeeImage, true, 1, 1, displayHeight);
                append(leftInsects[i]);
            }
        }
        if (rightInsects == null) {
            rightInsects = new Insect[level];
            for (int i = 0; i < rightInsects.length; i++) {
                rightInsects[i] = new Insect(MosImage,BeeImage, false, 1, 1, displayHeight);
                append(rightInsects[i]);
            }
        }
   
        if (getTongue() == null) {
            setTongue(new Tongue[10 + tonguelength*5]);
            for (int i = 0; i < getTongue().length; i++) {
                getTongue()[i] = new Tongue(tongueImageBody, displayWidth, displayHeight);
                append(getTongue()[i]);
            }

        }


    }


    public void setPetRotation(int x, int y) {
        getPet().SetPetRotation(x, y);
    }

    public void paint(Graphics g) throws Exception {

        setViewWindow(0, 0, displayWidth, displayHeight);
        paint(g, cornerX, cornerY);

    }

    public int updateLayers(int gameTicks) {
        gameTime++;
        int level = gameTime / 500 + 1;
        if (level > 4)
            level = 4;
        
        for (int i = 0; i < level; i++) {
            leftInsects[i].Update(gameTime, 0, displayWidth);
            if (gameTime % 500 == 499 && index % 2 == 0 && index < leftInsects.length)
            {
                leftInsects[index].setType(Insect.TYPE_BEE);  
                index++;
            }        
        }
        for (int i = 0; i < level; i++) {
            rightInsects[i].Update(gameTime, 0, displayWidth);
            if (gameTime % 500 == 499 && index % 2 != 0 && index < leftInsects.length)
            {
                rightInsects[index].setType(Insect.TYPE_BEE);
                index++;
            } 
        }
        getPet().UpdateInGame(gameTime);

        // tinh cai luoi 
        int dem = 0;
        if (isTongueActive) {
            for (dem = 0; dem < tongues.length; dem++) {
                if (tongues[dem].isVisible() == false) {
                    break;
                }
            }

            // check collision
            if (dem - 1 >= 0) {

                for (int i = 0; i < leftInsects.length; i++) {
                    boolean flag = false;
                    for (int j = 0; j < pet.getSpeed(); j++)
                    {
                        if (dem-1-j>=0 && leftInsects[i].collidesWith(tongues[dem - 1 - j], false))
                               flag = true; 
                    }
                    if (leftInsects[i].isVisible() && flag == true
                            && (Collide == false || leftInsects[i].isIsCollision())) {
                        leftInsects[i].setIsCollision(true);
                        int newPos = dem - pet.getSpeed() - 5 - 2;
                        if (newPos < 0) {
                            newPos = 0;
                        }
                        int newXPos = tongues[newPos].getRefPixelX();
                        int newYPos = tongues[newPos].getRefPixelY();
                        leftInsects[i].setRefPixelPosition(newXPos, newYPos);
                        setTongueReturn(true);
                        Collide = true;
                        if (pet.collidesWith(leftInsects[i], false)) {
                            leftInsects[i].setVisible(false);
                            leftInsects[i].setIsCollision(false);
                            Collide = false;
                            gameTicks = gameTicks + leftInsects[i].GetScore();
                            setScore(leftInsects[i].GetScore());
                            setNoOfInsect(getNoOfInsect() + 1);
                    
                        }
                    }
                }
                for (int i = 0; i < rightInsects.length; i++) {
                    boolean flag = false;
                    for (int j = 0; j < pet.getSpeed(); j++)
                    {
                        if ( dem-1-j>=0 && rightInsects[i].collidesWith(tongues[dem - 1 - j], false))
                               flag = true; 
                    }
                    if (rightInsects[i].isVisible() && flag == true
                    && (Collide == false || rightInsects[i].isIsCollision())) {
                        rightInsects[i].setIsCollision(true);
                        int newPos = dem - pet.getSpeed() - 5 - 2;
                        if (newPos < 0) {
                            newPos = 0;
                        }
                        int newXPos = tongues[newPos].getRefPixelX();
                        int newYPos = tongues[newPos].getRefPixelY();
                        rightInsects[i].setRefPixelPosition(newXPos, newYPos);
                        setTongueReturn(true);
                        Collide = true;
                        if (pet.collidesWith(rightInsects[i], false)) {
                            rightInsects[i].setVisible(false);
                            rightInsects[i].setIsCollision(false);
                            Collide = false;
                            gameTicks = gameTicks + rightInsects[i].GetScore();
                            setScore(rightInsects[i].GetScore());
                            setNoOfInsect(getNoOfInsect() + 1);
                         
                        }
                    }
                }

            }


            if (isTongueReturn() == false) {
                if (dem != tongues.length) {
                    for (int i = 0; i < pet.getSpeed(); i++) {
                        if (dem + i < tongues.length) {
                            tongues[dem + i].setVisible(true);

                        }
                    }


                } else {
                    TongueReturn = true;

                }
            } else { // neu nhu la quay nguoc tro lai
                Collide = true;
                if (dem != 0) {
                    for (int i = 0; i < pet.getSpeed() + 5; i++) {
                        if (dem - i >= 0 && dem - i < tongues.length) {
                            tongues[dem - i].setVisible(false);

                        }
                    }
                } else {
                    TongueReturn = false;
                    setIsTongueActive(false);
                    Collide = false;
                    pet.setImage(petNormal, petNormal.getWidth(), petNormal.getHeight());
                }
            }
        }
        return gameTicks;
    }

    public PlayPet getPet() {
        return pet;
    }

    /**
     * @param pet the pet to set
     */
    public void setPet(PlayPet pet) {
        this.pet = pet;
    }

    public Tongue getTongue(int i) {
        if (i < getTongue().length) {
            return getTongue()[i];
        }
        return null;
    }

    /**
     * @return the tongue
     */
    public Tongue[] getTongue() {
        return tongues;
    }

    /**
     * @param tongue the tongue to set
     */
    public void setTongue(Tongue[] tongue) {
        this.tongues = tongue;
    }

    /**
     * @return the isTongueActive
     */
    public boolean isIsTongueActive() {
        return isTongueActive;
    }

    /**
     * @param isTongueActive the isTongueActive to set
     */
    public void setIsTongueActive(boolean isTongueActive) {
        this.isTongueActive = isTongueActive;
    }

    /**
     * @return the TongueReturn
     */
    public boolean isTongueReturn() {
        return TongueReturn;
    }

    /**
     * @param TongueReturn the TongueReturn to set
     */
    public void setTongueReturn(boolean TongueReturn) {
        this.TongueReturn = TongueReturn;
    }

    /**
     * @return the NoOfInsect
     */
    public int getNoOfInsect() {
        return NoOfInsect;
    }

    /**
     * @param NoOfInsect the NoOfInsect to set
     */
    public void setNoOfInsect(int NoOfInsect) {
        this.NoOfInsect = NoOfInsect;
    }

    /**
     * @return the Score
     */
   

    /**
     * @param Score the Score to set
     */
    public void setScore(int Score) {
        this.Score = Score;
    }

    
    public int GetScoreInsect()
    {
        int temp = Score;
        if (Score != -1){
            Score = -1;
        }
        return temp;
        
    }

    
}
